<?php

namespace App\Models;

use App\Libs\DataFormat;
use EasyWeChat\Foundation\Application;
use EasyWeChat\Payment\Order;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class UserTrans extends Model
{

    use DataFormat;

    protected $table = 'user_trans';
    protected $dates = [''];

    protected $guarded = ['id', 'created_at'];
    protected $fillable = ['user_id', 'org_id', 'type', 'amount', 'sn', 'desc', 'status', 'prepay_id', 'pay_time'];

    protected $casts = [

        'desc'=>'array'
    ];



    public static function joinProject($orgId, $userInfo, $amount, $desc)
    {
        $orgInfo = Org::find($orgId);

        $options = config('wechat');
        $options['app_id'] = $orgInfo->wechat_app_id;
        $options['secret'] = $orgInfo->wechat_app_secret;
        $options['payment']['merchant_id'] = $orgInfo->wechat_payment_merchant_id;
        $options['payment']['key'] = $orgInfo->wechat_payment_key;

        $app = new  Application($options);
        $payment = $app->payment;
        $js = $app->js;

        $attr['device_info'] = 'webchat';
        $attr['nonce_str'] = md5(uniqid());
        $attr['body'] = '志愿商品';
        $attr['out_trade_no'] = UserTrans::createOrdId();
        $attr['total_fee'] =  $amount * 100;
        $attr['spbill_create_ip'] = request()->getClientIp();
        $attr['openid'] = $userInfo->open_id;
        $attr['trade_type'] = 'JSAPI';
        $attr['notify_url'] = url('/api/v1/wechat/callback');


        $order = new Order($attr);

        $result = $payment->prepare($order);
        if ($result->return_code != 'SUCCESS' || $result->result_code != 'SUCCESS'){

            $rs['err_code'] = 20001;
            $rs['err_msg'] = '微信下单失败,'.$result->return_msg;
            return $rs;
        }

        $prepayId = $result->prepay_id;

        $payConfig = $payment->configForJSSDKPayment($prepayId);
        $jsConfig = $js->config(array('onMenuShareQQ', 'onMenuShareWeibo', 'chooseWXPay'), false);
        $jsConfig = json_decode($jsConfig, true);
        unset($jsConfig['url']);
        $jsConfig = json_encode($jsConfig);

        $userTrans = new UserTrans();
        $userTrans->fill([
            'org_id'=>$orgId,
            'user_id'=>$userInfo->id,
            'sn'=>$attr['out_trade_no'],
            'type'=>0,
            'amount'=> $amount,
            'status'=>0,
            'prepay_id'=>$result->prepay_id,
            'desc'=>$desc,
        ]);


        if($userTrans->save())
        {
            $data['err_code'] = 0;
            $data['jsConfig'] = json_decode($jsConfig, true);
            $data['payConfig'] = $payConfig;
            $data['sn'] = $userTrans->sn;
            $data['project_id'] = $desc['project_id'];

            return $data;
        }

        $rs['err_code'] = 10001;
        $rs['err_msg'] = '微信下单失败,错误的下单配置';

        return $rs;

    }

    public static  function createOrdId(){

        return date('ymd').sprintf('%06d', rand(0, 999999));
    }

}
